package com.weixin.paleituo.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.SaleStatusArrowRecyclerViewAdapter;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.eventbus.CheckCityEvent;
import com.weixin.paleituo.eventbus.CheckLeaseHouseEvent;
import com.weixin.paleituo.eventbus.CheckSaleStatueCityEvent;
import com.weixin.paleituo.util.UIUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Author yichao
 * Time  2017/12/10 20:06
 * Dest  选择地区
 */

public class AlertPopuCommon extends PopupWindow {

    private View rootView;
    private Context context;
    private String mDialogType;
    private List<String> mMStringList;
    private String mChekStr;
    private RecyclerView mRecyclerView;
    private SaleStatusArrowRecyclerViewAdapter mSaleStatusArrowRecyclerViewAdapter;
    private RelativeLayout mRlcontenetRootView;

    public AlertPopuCommon(Context context, String dialogType, List<String> mStringList, String chekStr) {
        super(context);
        this.context = context;
        mDialogType = dialogType;
        mMStringList = mStringList;
        mChekStr = chekStr;
        init();
    }

    private void init() {
        rootView = LayoutInflater.from(context).inflate(R.layout.arrow_popuwindow_layout, null);
        // TODO: 2016/5/17 设置动画
      //  setAnimationStyle(R.style.popwin_anim_style);
        // TODO: 2016/5/17 设置背景颜色
      setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33000000")));
        // TODO: 2016/5/17 设置可以获取焦点
      setFocusable(true);
        // TODO: 2016/5/17 设置可以触摸弹出框以外的区域
      setOutsideTouchable(true);
        // TODO：更新popupwindow的状态
        setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);

        setContentView(rootView);

        mRecyclerView = rootView.findViewById(R.id.recycle_arrow_activity_sale_status);
        mRlcontenetRootView = rootView.findViewById(R.id.ll_contentroot_arrow_popuwindow_layout);

        if (mMStringList.size()>6){
            ViewGroup.LayoutParams layoutParams = mRecyclerView.getLayoutParams();
            layoutParams.height= UIUtils.getScreenHeigh(context)/2;
            mRecyclerView.setLayoutParams(layoutParams);
        }

        mSaleStatusArrowRecyclerViewAdapter = new SaleStatusArrowRecyclerViewAdapter(context, mMStringList);
        mSaleStatusArrowRecyclerViewAdapter.setCheckData(mChekStr);
        //设置布局管理器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        //设置adapter
        mRecyclerView.setAdapter(mSaleStatusArrowRecyclerViewAdapter);
        //设置Item增加、移除动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //添加分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(
                context, DividerItemDecoration.VERTICAL));
        mSaleStatusArrowRecyclerViewAdapter.setOnItemClickListener(new SaleStatusArrowRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onClick(View v, int position) {
                if (mDialogType == Contans.DIAG_TYPE_CITY) {
                    EventBus.getDefault().post(new CheckCityEvent(mMStringList.get(position)));
                }
                if (mDialogType == Contans.DIAG_TYPE_HOUSE_SALE_STATUE) {
                    EventBus.getDefault().post(new CheckSaleStatueCityEvent(mMStringList.get(position)));

                }

                if (mDialogType == Contans.DIAG_TYPE_SELECT_HOUSE) {
                    EventBus.getDefault().post(new CheckLeaseHouseEvent(mMStringList.get(position)));

                }

                mSaleStatusArrowRecyclerViewAdapter.setCheckData(mMStringList.get(position));

                mSaleStatusArrowRecyclerViewAdapter.notifyDataSetChanged();
                dismiss();
            }
        });
        mRlcontenetRootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    public void showDialog() {
        init();
    }

    public View getView() {
        return rootView;
    }

    public void setChekStr(String checkStr) {

        mChekStr = checkStr;
    }

    public void setList(List<String> stringList) {
        mMStringList = stringList;
        if (mMStringList.size()>6){
            ViewGroup.LayoutParams layoutParams = mRecyclerView.getLayoutParams();
            layoutParams.height= UIUtils.getScreenHeigh(context)/2;
            mRlcontenetRootView.setLayoutParams(layoutParams);
        }
        mSaleStatusArrowRecyclerViewAdapter.notifyDataSetChanged();

    }
    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24){
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if(Build.VERSION.SDK_INT >= 24){
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
        }


        super.showAsDropDown(anchor, xoff, yoff);
    }

}
